{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\socks.py:58: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working\n",
      "  from collections import Callable\n",
      "C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\scipy\\linalg\\__init__.py:212: DeprecationWarning: The module numpy.dual is deprecated.  Instead of using dual, use the functions directly from numpy or scipy.\n",
      "  from numpy.dual import register_func\n",
      "C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\scipy\\special\\orthogonal.py:81: DeprecationWarning: `np.int` is a deprecated alias for the builtin `int`. To silence this warning, use `int` by itself. Doing this will not modify any behavior and is safe. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the release note link for additional information.\n",
      "Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n",
      "  from numpy import (exp, inf, pi, sqrt, floor, sin, cos, around, int,\n",
      "C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\scipy\\sparse\\sputils.py:16: DeprecationWarning: `np.typeDict` is a deprecated alias for `np.sctypeDict`.\n",
      "  supported_dtypes = [np.typeDict[x] for x in supported_dtypes]\n",
      "C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\matplotlib\\colors.py:53: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working\n",
      "  from collections import Sized\n",
      "2025-05-26 11:36:58,893-WARNING: type object 'QuantizationTransformPass' has no attribute '_supported_quantizable_op_type'\n",
      "2025-05-26 11:36:58,894-WARNING: If you want to use training-aware and post-training quantization, please use Paddle >= 1.8.4 or develop version\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-05-26 11:37:01 [INFO]\tStarting to read file list from dataset...\n",
      "2025-05-26 11:37:01 [INFO]\t1260 samples in file ./train.txt\n",
      "2025-05-26 11:37:01 [INFO]\tStarting to read file list from dataset...\n",
      "2025-05-26 11:37:01 [INFO]\t317 samples in file ./val.txt\n"
     ]
    }
   ],
   "source": [
    "import paddlex as pdx\n",
    "from paddlex import transforms\n",
    "train_transforms=transforms.Compose([transforms.RandomCrop(crop_size=224),transforms.RandomHorizontalFlip(),\n",
    "                                    transforms.RandomDistort(brightness_range=0.9,brightness_prob=0.5,\n",
    "                                                           contrast_range=0.9,contrast_prob=0.5,\n",
    "                                                           saturation_range=0.9,saturation_prob=0.5,\n",
    "                                                           hue_range=18,hue_prob=0.5),\n",
    "                                    transforms.Normalize()])\n",
    "val_transforms=transforms.Compose([transforms.ResizeByShort(short_size=256),\n",
    "                                  transforms.CenterCrop(crop_size=224),\n",
    "                                  transforms.Normalize()])\n",
    "train_dataset=pdx.datasets.ImageNet(\n",
    "    data_dir='garbage',\n",
    "    file_list='./train.txt',\n",
    "    label_list='./labels.txt',\n",
    "    transforms=train_transforms,\n",
    "    shuffle=True\n",
    ")\n",
    "val_dataset=pdx.datasets.ImageNet(\n",
    "    data_dir='garbage',\n",
    "    file_list='./val.txt',\n",
    "    label_list='./labels.txt',\n",
    "    transforms=val_transforms\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import paddlex as pdx\n",
    "num_classes=len(train_dataset.labels)\n",
    "model=pdx.cls.ResNet50_vd_ssld(num_classes=num_classes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-05-26 11:37:10 [INFO]\tDownloading ResNet50_vd_ssld_pretrained.pdparams from https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/legendary_models/ResNet50_vd_ssld_pretrained.pdparams\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████████████████████████████████████| 151509/151509 [00:32<00:00, 4729.08KB/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-05-26 11:37:43 [INFO]\tLoading pretrained model from output/ResNet50_vd_ssld\\pretrain\\ResNet50_vd_ssld_pretrained.pdparams\n",
      "2025-05-26 11:37:43 [WARNING]\t[SKIP] Shape of pretrained params fc.weight doesn't match.(Pretrained: (2048, 1000), Actual: [2048, 3])\n",
      "2025-05-26 11:37:43 [WARNING]\t[SKIP] Shape of pretrained params fc.bias doesn't match.(Pretrained: (1000,), Actual: [3])\n",
      "2025-05-26 11:37:43 [INFO]\tThere are 275/277 variables loaded into ResNet50_vd_ssld.\n"
     ]
    }
   ],
   "source": [
    "model.train(num_epochs=5,\n",
    "           train_dataset=train_dataset,\n",
    "           train_batch_size=16,\n",
    "           eval_dataset=val_dataset,\n",
    "           lr_decay_epochs=[80,100,150],\n",
    "           save_interval_epochs=1,\n",
    "           learning_rate=0.002,\n",
    "           save_dir='output/ResNet50_vd_ssld',\n",
    "           use_vdl=True)\n",
    "model=pdx.load_model('output/ResNet50_vd_ssld/best_model')\n",
    "image_name='./data/garbage/paper/paper10.jpg'\n",
    "result=model.predict(image_name)\n",
    "print('Predict Result:',result)\n",
    "number=result[0]['category']\n",
    "number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
